/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var notify=require('../models/notify.js');
var viruser=require('../models/viruser.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }
    if(request.session.roleinfo.flag!==99)
    {
        response.send("<script>window.parent.location.href='index.html';</script>");
        return;
    }

    var notifydata=RUtil.notifydata(request);
    notifydata.retdata.nickname =request.session.admin.nickname;
    notifydata.retdata.roleinfo = request.session.roleinfo;
    notifydata.retdata.userinfo = request.session.admin;
    notifydata.retdata.title="通知/告警管理";
    if(method.toUpperCase() === "GET") {


        notify.selectAll( function(error,results) {
            if((error)!== null)
            {
                // 数据库异常
                notifydata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                notifydata.retdata.success=false;
                response.render("notifyadmin", notifydata);

            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                notifydata.retdata.info = "没有任何通知记录!";
                notifydata.retdata.success=false;
                response.render("notifyadmin", notifydata);
            } else {


                //存在数据将数据发到页面
                notifydata.retdata.info = "通知提取成功!";
                notifydata.retdata.success=true;
                notifydata.notifys= results;

                response.render("notifyadmin", notifydata);
            }
        });
    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper == "del")
        {
            var delid = request.body.id;
            notify.deleteByID(delid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"success",info:'删除失败：系统内部错误(错误码：'+error.errno+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'删除失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'删除成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


